<cftry>

  <cfset myArray = arrayNew(1) />

    <cfset a = arrayNew(1) />
    <cfloop from="1" to="100" index="j">
     <cfset a[1] = randDate() />
     <cfset a[2] = randDate() />
     <cfset a[3] = randDate() />
     <cfset a[4] = randRange(0,100) />
     <cfset a[5] = randRange(0,100) />
     <cfset a[6] = randRange(0,100) />
     <cfset a[7] = lcase(randString("alpha",randRange(0,10))) />
     <cfset a[8] = lcase(randString("alpha",randRange(0,10))) />
     <cfset a[9] = lcase(randString("alpha",randRange(0,10))) />
     <cfset a[10] = randRange(0,100) />
     <cfset a[11] = randDate() />


      <cfset x = structNew() />
      <cfset x.name = a[randrange(7,11)] />
      <cfset x.number = a[randrange(1,9)] />
      <cfset x.date = a[randrange(1,9)] />
      <cfset arrayAppend(myArray,x) />
    </cfloop>

  <cfset flu = createObject("component","org.coldquery.ColdQuery").init(myArray) />

  <cfset items = flu.getItems() >

<cfoutput>

<hr>Show Original Array
<cfset items = flu.getItems() >
<table>
    <tr>
      <td>name</td>
      <td>number</td>
      <td>date</td>
    </tr>
  <cfloop from="1" to="#arrayLen(items)#" index="i">
    <tr>
      <td><cfdump var="#items[i].name#"></td>
      <td>#items[i].number#</td>
      <td>#items[i].date#</td>
    </tr>
  </cfloop>
</table>

<hr>Test Selection Functions - Date
<cfset flu = flu.$("function f(obj){
                      return isDate(obj.date);
                    }
                  ") >
<cfset items = flu.getItems() >
<table>
    <tr>
      <td>name</td>
      <td>number</td>
      <td>date</td>
    </tr>
  <cfloop from="1" to="#arrayLen(items)#" index="i">
    <tr>
      <td><cfdump var="#items[i].name#"></td>
      <td>#items[i].number#</td>
      <td>#items[i].date#</td>
    </tr>
  </cfloop>
</table>

<hr>Test Selection Functions - number
<cfset flu = flu.$("function f(obj){
                      return isnumeric(obj.number);
                    }
                   ") >
<cfset items = flu.getItems() >
<table>
    <tr>
      <td>name</td>
      <td>number</td>
      <td>date</td>
    </tr>
  <cfloop from="1" to="#arrayLen(items)#" index="i">
    <tr>
      <td><cfdump var="#items[i].name#"></td>
      <td>#items[i].number#</td>
      <td>#items[i].date#</td>
    </tr>
  </cfloop>
</table>

<hr>Test Selection Functions - name
<cfset flu = flu.$("function f(obj){
                      return (len(trim(obj.name)) GTE 1 AND NOT isNumeric(obj.name) AND NOT isDate(obj.name))
                        ;}
                   ") >
<cfset items = flu.getItems() >
<table>
    <tr>
      <td>name</td>
      <td>number</td>
      <td>date</td>
    </tr>
  <cfloop from="1" to="#arrayLen(items)#" index="i">
    <tr>
      <td><cfdump var="#items[i].name#"></td>
      <td>#items[i].number#</td>
      <td>#items[i].date#</td>
    </tr>
  </cfloop>
</table>

<!--- test sorting functions --->
<hr>Test Sorting Functions - Date
<cfset flu = flu.sortByColumn("date","date","asc") >
<cfset items = flu.getItems() >
<table>
    <tr>
      <td>name</td>
      <td>number</td>
      <td>date</td>
    </tr>
  <cfloop from="1" to="#arrayLen(items)#" index="i">
    <tr>
      <td><cfdump var="#items[i].name#"></td>
      <td>#items[i].number#</td>
      <td>#items[i].date#</td>
    </tr>
  </cfloop>
</table>

<hr>Test Sorting Functions - Sort by Name using a custom sort function
<cfset flu = flu.sortByColumn("name",mycomparefunction,"asc") >
<cfset items = flu.getItems() >
<table>
    <tr>
      <td>name</td>
      <td>number</td>
      <td>date</td>
    </tr>
  <cfloop from="1" to="#arrayLen(items)#" index="i">
    <tr>
      <td><cfdump var="#items[i].name#"></td>
      <td>#items[i].number#</td>
      <td>#items[i].date#</td>
    </tr>
  </cfloop>
</table>

<hr>Test Sorting Functions number
<cfset flu = flu.sortBycolumn("number","numeric","asc") >
<cfset items = flu.getItems() >
<table>
    <tr>
      <td>name</td>
      <td>number</td>
      <td>date</td>
    </tr>
  <cfloop from="1" to="#arrayLen(items)#" index="i">
    <tr>
      <td><cfdump var="#items[i].name#"></td>
      <td>#items[i].number#</td>
      <td>#items[i].date#</td>
    </tr>
  </cfloop>
</table>

<hr>Test reverse Functions
<cfset flu = flu.reverseItems() >
<cfset items = flu.getItems() >
<table>
    <tr>
      <td>name</td>
      <td>number</td>
      <td>date</td>
    </tr>
  <cfloop from="1" to="#arrayLen(items)#" index="i">
    <tr>
      <td><cfdump var="#items[i].name#"></td>
      <td>#items[i].number#</td>
      <td>#items[i].date#</td>
    </tr>
  </cfloop>
</table>

<hr>Test chaining Functions
<cfset flu = flu.init(myArray) >
<cfset flu = flu.$("function f(obj) {return isDate(obj.date);}").
        $("function f(obj){return isNumeric(obj.number);}").
        sortByColumn("number","numeric","asc") />
<cfset items = flu.getItems() >
<table>
    <tr>
      <td>name</td>
      <td>number</td>
      <td>date</td>
    </tr>
  <cfloop from="1" to="#arrayLen(items)#" index="i">
    <tr>
      <td><cfdump var="#items[i].name#"></td>
      <td>#items[i].number#</td>
      <td>#items[i].date#</td>
    </tr>
  </cfloop>
</table>


<hr>Test Custom Sort Function
<cfset flu = flu.init(myArray) >
<cfset flu = flu.$("function f(obj) {return isDate(obj.date);}").
        $("function f(obj){return isNumeric(obj.number);}") />
<cfset flu = flu.sortCustom("function f(a,b){
                              if(not isStruct(a) OR not isStruct(b)){
                                return 2;
                              }
                              if(not isnumeric(a.number) OR not isnumeric(b.number)){
                                return 1;
                              }
                              if(a.number LT b.number){
                                return -1;
                              }
                              else if(a.number EQ b.number){
                                return 0;
                              }
                              else return 1;
                            }","asc") />
<cfset items = flu.getItems() >
<table>
    <tr>
      <td>name</td>
      <td>number</td>
      <td>date</td>
    </tr>
  <cfloop from="1" to="#arrayLen(items)#" index="i">
    <tr>
      <td><cfdump var="#items[i].name#"></td>
      <td>#items[i].number#</td>
      <td>#items[i].date#</td>
    </tr>
  </cfloop>
</table>


</cfoutput>
  <cfcatch>
    <cfdump var="#cfcatch#" />
  </cfcatch>
</cftry>


<cfscript>
/**
 * Returns a random string of the specified length of either alpha, numeric or mixed-alpha-numeric characters.
 * v2, support for lower case
 * v3 - more streamlined code
 *
 * @param Type 	 Type of random string to create. (Required)
 * @param Length 	 Length of random string to create. (Required)
 * @return Returns a string.
 * @author Joshua Miller (josh@joshuasmiller.com)
 * @version 2, November 4, 2003
 */
function randString(type,ct){
 var i=1;
 var randStr="";
 var randNum="";
 var useList="";
 var alpha="A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
 var secure="!,@,$,%,&,*,-,_,=,+,?,~";
 for(i=1;i LTE ct;i=i+1){
  if(type is "alpha"){
   randNum=RandRange(1,52);
   useList=alpha;
  }else if(type is "alphanum"){
   randNum=RandRange(1,62);
   useList="#alpha#,0,1,2,3,4,5,6,7,8,9";
  }else if(type is "secure"){
   randNum=RandRange(1,73);
   useList="#alpha#,0,1,2,3,4,5,6,7,8,9,#secure#";
  }else{
   randNum=RandRange(1,10);
   useList="0,1,2,3,4,5,6,7,8,9";
  }
  randStr="#randStr##ListGetAt(useList,randNum)#";
 }
 return randStr;
}

function randDate(){
  return randRange(1,12) & "/" & randRange(1,28) & "/" & randRange(2001,2008);
}

</cfscript>



  <cffunction name="mycomparefunction" output="false">
    <cfargument name="node1" required="true" />
    <cfargument name="node2" required="true" />
    <cfset var f1 = arguments.node1 />
    <cfset var f2 = arguments.node2 />
    <cfset var result = 0 />
    <cfif isSimpleValue(f1) and isSimpleValue(f2)>
      <cfset result = compareNoCase(f1,f2) />
    <cfelseif isSimpleValue(f1)>
      <cfset result = -1 /><!--- can only compare simple values --->
    <cfelse>
      <cfset result = 1 /><!--- can only compare simple values --->
    </cfif>
    <cfreturn result />
  </cffunction>


